{include file="../application/admin/view/header.html" /}
</head>
<body>
<div class="container">
	<div class="col-md-12">
		<div class="alert kesn_prompt"></div>
	</div>

	<div class="row">
		<button class="btn btn-info add_submodules">新增子模块<span class="fui-plus btn-tip"></span></button>
	</div><br/>
	<div class="row input-group">
	  <span class="input-group-addon">模块名</span>
	  <input type="text" class="form-control" placeholder="" name="module"/>
	</div>
	
	&nbsp;&nbsp;<br/>
	<div></div>
	<div class="row">
		<table class="table table-hover">
			<thead>
				<th>#</th>
				<th class="col-sm-2 col-md-2">子模块</th>
				<th class="col-sm-2 col-md-2">data-id</th>
				<th>url</th>
				<th>操作</th>
			</thead>
			<tbody>
			
			<tr class="_item">
					<th scrope="row"></th>
					<td><input type="text" class="form-control input-sm" placeholder="" name="submodule"/></td>
					<td><input type="text" class="form-control input-sm" placeholder="" name="data-id"/></td>
					<td><input type="text" class="form-control input-sm" placeholder="" name="url"/></td>
					<td><div>
						<a href="#" class="btn btn-danger del_submodule btn-sm">删除<span class="fui-cross btn-tip"></span></a>
					</div></td>
			</tr>
		</tbody>
		</table>
	</div>
	<button class="btn btn-info module_save">保存</button>
</div>

{include file="../application/admin/view/footer.html" /}
<script type="text/javascript">
//新增子模块
$(".row").on('click','.add_submodules',function(){
	var _html = '<tr class="_item"><th scrope="row"></th><td><input type="text" class="form-control input-sm" placeholder="" name="submodule"/></td><td><input type="text" class="form-control input-sm" placeholder="" name="data-id"/></td><td><input type="text" class="form-control input-sm" placeholder="" name="url"/></td><td><div><a href="#" class="btn btn-danger del_submodule btn-sm">删除<span class="fui-cross btn-tip"></span></a></div></td></tr>';
	$("tbody").append(_html);
});
//删除子模块
$('tbody').on('click','.del_submodule',function(){
	var _this = $(this);
	var this_item = _this.parents('._item');
	this_item.remove();
});

//新增模块
$(".container").on('click','.module_save',function(){
	var _this = $(this);
	var dataArr = {};
	dataArr['module'] = $('input[name=module]').val();
	if (dataArr['module'] == '') {
		kesn_prompt('fail','请填写模块名！');
		return;
	}
	dataArr['submodule'] ={};
	$("._item").each(function(i){
		var this_i = $(this);
		arr_temp = {};
		arr_temp['submodule'] = this_i.find('input[name=submodule]').val();
		arr_temp['data_id'] = this_i.find('input[name=data-id]').val();
		arr_temp['url'] = this_i.find('input[name=url]').val();
		dataArr['submodule'][i] =arr_temp;
	});
	for (var i = 0; i < dataArr.length; i++) {
		for(var key in dataArr[i]){
			if (dataArr[i][key] == '') {
				kesn_prompt('fail','请完全填充资料');
				return;
			}
		}
	}
	$.ajax({
			url : 'add',
			async:true,
			type:'POST',
			data:dataArr,
			dataType:'JSON',
			success:function(data,textStatus,jqXHR){
    				if (data.err != '0') {
    					kesn_prompt('fail',data.content);
    				}else if (data.err == '0') {
                        kesn_prompt('success',data.content);
                        _this.attr('disabled','disabled');
                    }

    			},
    			error:function(){
    				kesn_prompt('fail','网络出错');
    			}
		});
});
</script>
</footer>
</body>
</html>